package modbus.com.serialport;


import com.fazecast.jSerialComm.SerialPort;
import com.fazecast.jSerialComm.SerialPortDataListener;
import com.fazecast.jSerialComm.SerialPortEvent;

/**
 *串口监听
 */
public class SerialPortListener implements SerialPortDataListener {

    private final SerialPortSend serialPortSend;
    //添加监听串口
    public SerialPortListener(SerialPortSend serialPortCallback) {
        this.serialPortSend = serialPortCallback;
    }

    @Override
    public int getListeningEvents() { //必须是return这个才会开启串口工具的监听
        return SerialPort.LISTENING_EVENT_DATA_AVAILABLE;
    }
    //添加监听串口数据返回的数据
    @Override
    public void serialEvent(SerialPortEvent serialPortEvent) {
        if (serialPortSend != null) {
            //返回数据的接口
            serialPortSend.dataAvailable();
        }
    }
}

